package com.wryyyyy.config;

import com.wryyyyy.utils.AdminInterceptor;
import com.wryyyyy.utils.LoginInterceptor;
import com.wryyyyy.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // token刷新的拦截器
        // order数字越小，越先执行
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(1);
        // 登录拦截器
//        registry.addInterceptor(new LoginInterceptor())
//                .excludePathPatterns(
//                        // 放行：
//                        "/dish/list",
//                        "/dish/{{id}}", //查询单个
////                        "/**",
//                        "/label/scan/list",
//                        "/label/scan/of/condition",
//                        "/material/list",
//                        "/user/code",
//                        "/user/login",
//                        "/user/loginPwd",
//                        "/user/me",
//                        "/images/**",
//                        "/dish/upload",
//                        "/icons/**",
//                        "/order/**",
//                        "label/**"
//                )
//                .order(2);
        // 管理员权限拦截器
//        registry.addInterceptor(new AdminInterceptor())
//                .addPathPatterns(
////                        "/dish/upload",
//                        "/dish/save",
//                        "/dish/update",
//                        "/dish/delete/{{id}}", // 删除单个
//                        "/dish/deleteBatch",
////                        "/dish/**",
//                        "/label/save",
//                        "/label/delete/{{id}}", // 删除单个
//                        "/label/deleteBatch",
//                        "/label/update",
//                        "/material/save",
//                        "/material/delete/{{id}}", // 删除单个
//                        "/material/deleteBatch",
//                        "/material/update"
//                ).order(3);
    }

    // 解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }
}